package babo.cd.physicalexperiment.activity;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.LinkedHashMap;

import babo.cd.physicalexperiment.BaseActivity;
import babo.cd.physicalexperiment.bean.JustCodeBean;
import babo.cd.physicalexperiment.data.MyEnventData;
import babo.cd.physicalexperiment.databinding.ActivityRegisterBinding;
import babo.cd.physicalexperiment.utils.OkUtils;
import babo.cd.physicalexperiment.utils.Out;
import babo.cd.physicalexperiment.utils.URLS;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class RegisterActivity extends BaseActivity {
    ActivityRegisterBinding binding;
    private int timec=60;
    CountDownTimer countDownTimer;

    @Override
    protected View bindView() {
        binding=ActivityRegisterBinding.inflate(getLayoutInflater());
        return binding.getRoot();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (countDownTimer!=null){
                countDownTimer.cancel();
                countDownTimer=null;
            }
        }catch (Exception ex){}
    }

    @Override
    public void setListener() {
        super.setListener();
        binding.registerBtn.setOnClickListener(view -> {
            if (TextUtils.isEmpty(binding.usernameEt.getText().toString().trim())){
                Out.toast(context,"请输入手机号！");
                return;
            }
            if (!Out.isPhone(binding.usernameEt.getText().toString().trim())){
                Out.toast(context,"请输入正确的手机号！");
                return;
            }
            if (TextUtils.isEmpty(binding.passwordEt.getText().toString().trim())||TextUtils.isEmpty(binding.passwordEt2.getText().toString().trim())){
                Out.toast(context,"请按要求输入密码！");
                return;
            }
            if (!binding.passwordEt2.getText().toString().trim().equals(binding.passwordEt.getText().toString().trim())){
                Out.toast(context,"两次输入的密码不一致！");
                binding.passwordEt2.setText("");
                return;
            }
            if (binding.passwordEt2.getText().toString().trim().length()<6){
                Out.toast(context,"密码长度不低于6位！");

                return;
            }
            doRegister();
        });
        binding.txYzm.setOnClickListener(view ->{
             countDownTimer=new CountDownTimer(10000,1000) {
                @Override
                public void onTick(long l) {
                    timec=timec-1;
                    if (timec<0){
                        timec=0;
                    }
                    binding.txYzm.setText(timec+"s");
                    binding.txYzm.setEnabled(false);
                }

                @Override
                public void onFinish() {
                    timec=10;
                    binding.txYzm.setEnabled(true);
                    binding.txYzm.setText("重新获取");
                }
                };
            countDownTimer.start();

        });

    }

    private void doRegister() {
        LinkedHashMap map=new LinkedHashMap();
        map.put("mobile",binding.usernameEt.getText().toString().trim());
        map.put("userName",binding.usernameEt.getText().toString().trim());
        map.put("password",binding.passwordEt.getText().toString().trim());;
Out.out("注册传参："+map);
        OkUtils.performByJsonArray(context, URLS.zhuce, map, 2, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Out.out("注册失败："+e.getMessage());

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
             String body=response.body().string();
             Out.out("注册返回："+body);
                JustCodeBean justCodeBean=gson.fromJson(body,JustCodeBean.class);
                if (justCodeBean.getCode()==0){
                    hd.post(new Runnable() {
                        @Override
                        public void run() {
                            Out.toast(context,"注册成功!请用该手机号登录！");
                            EventBus.getDefault().post(MyEnventData.getInstance("phone"+binding.usernameEt.getText().toString().trim()));
                             finish();
                        }
                    });

                }else {
                    hd.post(new Runnable() {
                        @Override
                        public void run() {
                            Out.toast(context,justCodeBean.getMsg());

                        }
                    });
                }
            }
        });

    }
}
